package org.thanos.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thanos.demo.model.Customer;
import org.thanos.demo.model.CustomerSearchRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

    // ES 操作类
    @Autowired
    private CustomerSearchRepository customerSearchRepository;

    private static AtomicLong atomicLong = new AtomicLong(0);

    @Override
    public void insert() {
        Customer customer = new Customer()
                .setId(atomicLong.getAndIncrement())
                .setCreateTime(System.currentTimeMillis())
                .setUuid(UUID.randomUUID().toString())
                .setName(UUID.randomUUID().toString());
        customerSearchRepository.save(customer);
    }

    @Override
    public void update() {
        Customer customer = new Customer();
        customer.setId(1L);
        customer.setName(UUID.randomUUID().toString());
        customerSearchRepository.save(customer);
    }

    @Override
    public void delete() {

    }

    @Override
    public Customer findOne() {
        return null;
    }

    @Override
    public List<Customer> findByPage() {
        return null;
    }

    @Override
    public List<Customer> findAll() {
        Iterable<Customer> customers = customerSearchRepository.findAll();
        List<Customer> resultList = new ArrayList<>();
        customers.forEach(resultList::add);
        return resultList;
    }

    @Override
    public Long queryCount() {
        return customerSearchRepository.count();
    }

}
